var Rson = require('Rson');

cc.Class({
    extends: cc.Component,

    properties: {
        // 激活状态表示目标棋子能够移动到这点
        canPass: false,
        posX:0,
        posY:0,

    },

    // use this for initialization
    onLoad: function () {
        
        let self = this;
        //缓存pfb
        cc.loader.loadRes("Prefab/GWall", (err, pfb) => {
            self.GWall = cc.instantiate(pfb);
            self.node.addChild(self.GWall);
            self.GWall.active = false;
        });
        cc.loader.loadRes("Prefab/BWall", (err, pfb) => {
            self.BWall = cc.instantiate(pfb);
            self.node.addChild(self.BWall);
            self.BWall.active = false;
        });
        cc.loader.loadRes("Prefab/shield", (err, pfb) => {
            self.shield = cc.instantiate(pfb);
            self.node.addChild(self.shield);
            self.shield.active = false;
        });
        cc.loader.loadRes("Prefab/RandomPos", (err, pfb) => {
            self.randomPos = cc.instantiate(pfb);
            self.node.addChild(self.randomPos);
            self.randomPos.active = false;
        });
        cc.loader.loadRes("Prefab/VerificationPos", (err, pfb) => {
            self.VerificationPos = cc.instantiate(pfb);
            self.node.addChild(self.VerificationPos);
            self.VerificationPos.active = false;
        });
        cc.loader.loadRes("Prefab/focus1", (err, prefab) => {
            self.MoveFocus = cc.instantiate(prefab);
            self.node.addChild(self.MoveFocus);
            self.MoveFocus.active = false;
        });
        this.main = cc.find('Canvas').getComponent('HelloWorld');
    	
    },
    // 设置该点是否能通过
    setCanPass: function(choose){
        if(choose && !this.canPass){

            this.canPass = true;
            let colorAction2 = cc.tintTo(2, 255, 255, 155);
            let colorAction1 = cc.tintTo(2, 200, 100, 255);
            let seq = cc.sequence(colorAction1, colorAction2).repeatForever();
            this.MoveFocus.runAction(seq);
            this.MoveFocus.active = true;

            this.MoveFocus.on('touchend', (event) => {
                // 选定棋子移动方向
                this.sendData({'code':'72', 'name':'move', data:{'x':this.posX, 'y':this.posY}});
                }, this.node);
        }else if(!choose && this.canPass){

            this.canPass = false;
            this.MoveFocus.active = false;
            this.MoveFocus.stopAllActions();
        }
    },

    // 设置/解除防火墙
    // @judge: 防火墙开关，true为开启， false为关闭
    // @group: 棋子阵营，'G'和'B'两种
    setFireWall: function(judge, group){
        if(judge && !this.hasLineBoost){
            this.hasFireWall = true;
            let fw = (group == 'G')?this.GWall : this.BWall;
            let shield = this.shield;
            fw.active = true;
            shield.active = true;

        }else if(!judge && this.hasFireWall){
            this.hasFireWall = false;
            let fw = (group == 'G')?this.GWall : this.BWall;
            let shield = this.shield;
            fw.active = false;
            shield.active = false;
        }
    },

    // 设置/解除随机验证
    //@mode: 0:隐藏 1:显示r按钮 2:同时显示r按钮和v缩放
    setRandomVerification: function(mode)
    {
        switch (mode) {
            case 0:
                this.randomPos.active = false;
                this.VerificationPos.active = false;
                this.VerificationPos.stopAllActions();
                break;
            case 1:
                this.randomPos.active = true;
                this.VerificationPos.active = false;
                this.VerificationPos.stopAllActions();
                break;
            case 2:
                this.randomPos.active = true;
                this.VerificationPos.active = true;
                let action = cc.sequence(cc.scaleTo(1,1,1.1), cc.scaleTo(1,1,1)).repeatForever();
                this.VerificationPos.runAction(action);
                break;
        };
    },

    // 发送消息给服务端
    sendData: function(cmd){
        this.main.sendData(cmd);
    }
});
